package com.LFLWZ.controller;

import com.LFLWZ.beans.Dto.Dto;
import com.LFLWZ.beans.POJO.*;
import com.LFLWZ.beans.Vo.ClassroomCourseVo;
import com.LFLWZ.beans.Vo.CourseByType;
import com.LFLWZ.beans.Vo.UserlogSheet;
import com.LFLWZ.beans.Vo.addLike;
import com.LFLWZ.service.*;
import com.LFLWZ.utils.DtoUtil;
import com.LFLWZ.utils.EmptyUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;



/**
 * (Menu)表控制层
 *
 * @author makejava
 * @since 2020-12-04 14:06:41
 */
@RestController
@RequestMapping("menu")
public class HomePageController {
    /**
     * 服务对象
     */
    @Reference
    private ShowPageService showPageService;
    @Reference
    private ClassroomCourseService classroomCourseService;
    @Reference
    private likeAndWorksService likeAndWorksService;
    @Reference
    private com.LFLWZ.service.worksService worksService;
    @Reference
    private com.LFLWZ.service.TabbarService tabbarService;
    @Reference
    private ClassroomCourseDetailsService classroomCourseDetailsService;
    @Reference
    private LogSheetService logSheetService;
    /**
     * 点赞方发
     * @param addLike
     * @return
     */
    @PostMapping("addLike")
    public Dto addLike(@RequestBody addLike addLike){
        Dto dto=null;
        LikeRecord likeRecord=new LikeRecord();
        int line=0;
        if(addLike.getWorksid()!=null&&addLike.getUid()!=null){
            dto=DtoUtil.returnError(4001,"参数不匹配");
        }
        likeRecord.setUid(addLike.getUid());
        likeRecord.setWorksid(addLike.getWorksid());
        List<LikeRecord> list=likeAndWorksService.selLike(likeRecord);
         if (EmptyUtils.isEmpty(list)){
             line=likeAndWorksService.addLike(addLike);
         }else {
            line=likeAndWorksService.deLike(likeRecord);
        }
        if(line>0){
            System.out.println(addLike.getWorksshow().getId()+"s"+addLike.getWorksshow().getLike());
            Worksshow Worksshow=likeAndWorksService.update(addLike.getWorksshow());
            if(EmptyUtils.isNotEmpty(Worksshow)){
                dto=DtoUtil.returnSuccessData(Worksshow);
            }else {
                dto=DtoUtil.returnError(4002,"点赞失败");
            }
        }else {
            dto=DtoUtil.returnError(4002,"点赞失败");
        }
        return dto;
    }

    /**
     * 查询用户点赞课程
     * @param uid
     * @return
     */
    @GetMapping("selLike/{uid}")
    public Dto selLike(@PathVariable Integer uid){
        Dto dto=null;
        LikeRecord likeRecord=new LikeRecord();
        if (uid!=null){
           dto= DtoUtil.returnError(4001,"用户uid不能为空");
        }
        likeRecord.setUid(uid);
        List<LikeRecord> likeRecords= likeAndWorksService.selLike(likeRecord);
        dto=DtoUtil.returnSuccessData(likeRecords);
        return dto;
    }
    /**
     * 查询课程目录
     * @param type
     * @return
     */
    @GetMapping("selCourseType/{type}")
    public Dto selCourseType(@PathVariable int type){

        Dto dto=null;
        CourseType courseType=new CourseType ();
        if (type !=0){
            courseType.setType(type);
        }
        List<CourseType> courseTypes= classroomCourseService.selCourseType(courseType);
        if (EmptyUtils.isEmpty(courseTypes)){
            dto=DtoUtil.returnError(4001,"查询失败");
        }else {
            dto=DtoUtil.returnSuccessData(courseTypes);
        }
        return dto;
    }
    @GetMapping("courseInfoByUid/{uid}")
    public Dto courseInfoByUid(@PathVariable Integer uid){
        System.out.println(uid);
        Dto dto=null;
        if (EmptyUtils.isEmpty(uid))
            dto=DtoUtil.returnError(4001,"数据错误");
        List<ClassroomCourse> courses =classroomCourseService.selByUid(uid);
        if (EmptyUtils.isEmpty(courses)){
            dto=DtoUtil.returnError(4002,"数据为空");
        }else {
            dto=DtoUtil.returnSuccessData(courses);
        }
        return dto;
    }
    /**
     * 课程实录测试数据
     * @param id
     * @return
     */
    @GetMapping("courseInfo/{id}")
    public Dto courseInfo(@PathVariable Integer id){
        Dto dto=null;
        /**
         * id 为null时默认查询推荐。0
         */
        if (id ==null){
            id=0;
        }

        List<ClassroomCourse> ccList=classroomCourseService.queryAll(id);
        if (EmptyUtils.isEmpty(ccList)){
            dto= DtoUtil.returnError(4001,"没有该数据");
        }else {
            dto=DtoUtil.returnSuccessData(ccList);
        }
        return dto;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @GetMapping("list")
    public Dto list(){
        return DtoUtil.returnSuccessData(showPageService.queryAllByLimit(0,6));
    }
    @GetMapping("slideshow")
    public Dto slideshow(){
        return DtoUtil.returnSuccessData(showPageService.queryAllByLimit(0,5));
    }
    @GetMapping("tab")
    public Dto tab(){
        return DtoUtil.returnSuccessData(tabbarService.queryAllByLimit(0,5));
    }
    @PostMapping("selCourseByType")
    public Dto selCourseByType(@RequestBody CourseByType courseByType){
        Dto dto=null;
        List<ClassroomCourse> courses=classroomCourseService.selFiltrate(courseByType);
        if (EmptyUtils.isEmpty(courses)){
            dto=DtoUtil.returnError(4001,"查询失败");
        }else {
            dto=DtoUtil.returnSuccessData(courses);
        }
        return dto;
    }
    @GetMapping("selFiltrate")
    public Dto selFiltrate(){
        Dto dto=null;
        List<filtrate> filtrates=classroomCourseService.sel_filtrate();
        if (EmptyUtils.isEmpty(filtrates))
            dto=DtoUtil.returnError(4001,"查询错误");
        if (EmptyUtils.isNotEmpty(filtrates))
            dto=DtoUtil.returnSuccessData(filtrates);
        return dto;
    }

    @GetMapping("selWorks/{name}")
    public Dto selWorks(@PathVariable String name){
        Dto dto=null;
        List<Worksshow> worksshows=null;
        Worksshow worksshow=new Worksshow();
        worksshow.setAudit(1);//审核通过的作品
        worksshow.setSpecial(1);//标识作品的标识符
        if (name.equals("0")){
            worksshows= worksService.selWorks(worksshow);
        }else {
            worksshow.setTags(name);
            worksshows=worksService.selWorks(worksshow);
        }

        if (EmptyUtils.isEmpty(worksshows)){
            dto=DtoUtil.returnError(4001,"查询失败");
        }else {
            dto=DtoUtil.returnSuccessData(worksshows);
        }
        return dto;
    }
    @GetMapping("selByStudyNumber")
    public Dto selByStudyNumber(){
        Dto dto=null;
        List<ClassroomCourse> list= classroomCourseService.selByStudyNumber();
        if (EmptyUtils.isEmpty(list))
            dto=DtoUtil.returnError(4001,"数据为空");
        else
            dto=DtoUtil.returnSuccessData(list);
        return dto;
    }
    @GetMapping("selById/{id}")
    public Dto selById(@PathVariable Integer id ){
        Dto dto=null;
        if (id==null)
            dto=DtoUtil.returnError(4001,"参数错误");
        ClassroomCourseVo classroomCourse=classroomCourseService.queryById(id);
        if (classroomCourse!=null)
            dto=DtoUtil.returnSuccessData(classroomCourse);
        else
            dto=DtoUtil.returnError(4002,"内容为空");
        return dto;
    }

    @GetMapping("selByCourseId/{id}")
    public Dto selByCourseId(@PathVariable Integer id){
        Dto dto=null;
        if (id==null){
            dto=DtoUtil.returnError(4001,"参数错误");
        }
        ClassroomCourseDetails classroomCourseDetails=new ClassroomCourseDetails();
        if (id!=0){
            classroomCourseDetails.setCourseid(id);
        }
        List<ClassroomCourseDetails> list= classroomCourseDetailsService.queryAll(classroomCourseDetails);
        if (EmptyUtils.isEmpty(list)){
            dto=DtoUtil.returnError(4002,"查询为空");
        }else {
            dto=DtoUtil.returnSuccessData(list);
        }
        return dto;
    }
    @PostMapping("addLog")
    public Dto addLog(@RequestBody LogSheet logSheet){
        Dto dto=null;
        if (logSheet==null){
            dto=DtoUtil.returnError(4001,"参数错误");
        }
        LogSheet log=null;
        LogSheet param=new LogSheet();
        param.setUid(logSheet.getUid());
        param.setAid(logSheet.getAid());
        List<LogSheet> logs= logSheetService.queryAll(param);
        if(EmptyUtils.isNotEmpty(logs)){
            logSheet.setId(logs.get(0).getId());
            log= logSheetService.update(logSheet);
        }else {
            log= logSheetService.insert(logSheet);
        }
        if (log==null)
            dto=DtoUtil.returnError(4002,"异常");
        else
            dto=DtoUtil.returnSuccessData(log);
        return dto;
    }
    @GetMapping("selLog/{uid}/{aid}")
    public Dto selLog(@PathVariable Integer uid,@PathVariable Integer aid){
        Dto dto=null;
        if (uid==null||aid==null)
            dto=DtoUtil.returnError(4001,"参数错误");
        LogSheet logSheet=new LogSheet();
        logSheet.setUid(uid);
        logSheet.setAid(aid);
        List<LogSheet> list= logSheetService.queryAll(logSheet);
        if (EmptyUtils.isNotEmpty(list)){
            String date=null;
            if(list.get(0).getTimeline()%60==0)
                date=(list.get(0).getTimeline()/60)+"分00";
            else
                date=list.get(0).getTimeline()/60+"分"+list.get(0).getTimeline()%60;
            list.get(0).setDatetime(date);
            dto=DtoUtil.returnSuccessData(list.get(0));
        }
        return dto;
    }

    @GetMapping("selLogByUid/{uid}")
    public Dto selLog(@PathVariable Integer uid){
        Dto dto=null;
        if (uid==null){
            dto=DtoUtil.returnError(4001,"参数错误");
        }
        List<UserlogSheet> userlogSheets= classroomCourseService.selLog(uid);
        if (EmptyUtils.isNotEmpty(userlogSheets)){
            for (UserlogSheet userlogSheet:userlogSheets){
                String date=null;
                if(userlogSheet.getTimeline()%60==0)
                    date=(userlogSheet.getTimeline()/60)+"分00";
                else
                    date=userlogSheet.getTimeline()/60+"分"+userlogSheet.getTimeline()%60;
                userlogSheet.setDate(date);
            }
            dto=DtoUtil.returnSuccessData(userlogSheets);
            System.out.println(userlogSheets.size());
        }else {
            dto=DtoUtil.returnError(4002,"查询为空");
        }
        return dto;
    }

    /**
     * 新增课程
     * @param classroomCourseVo
     * @return
     */
    @PostMapping("insert")
    public Dto insert(@RequestBody ClassroomCourseVo classroomCourseVo){
        Dto dto=null;
        if (classroomCourseVo==null){
            dto=DtoUtil.returnError(4001,"参数错误");
            return dto;
        }
        ClassroomCourseVo classroomCourse1=null;
        if(classroomCourseVo.getId()!=null){
            classroomCourse1=classroomCourseService.update(classroomCourseVo);
        }else {
            classroomCourse1= classroomCourseService.insert(classroomCourseVo);
            if (EmptyUtils.isNotEmpty(classroomCourseVo.getClassroomCourseDetails())){
                int i=1;
                for (ClassroomCourseDetails classroomCourseDetails:classroomCourseVo.getClassroomCourseDetails()){
                    if (classroomCourseDetails.getTitle()==null||classroomCourseDetails.getTitle().equals("")){
                        classroomCourseDetails.setTitle(classroomCourseVo.getCoursebrief());
                    }
                    if (classroomCourseDetails.getCourseid()==null){
                        classroomCourseDetails.setCourseid(classroomCourse1.getId());
                    }
                    if (classroomCourseDetails.getNumber()==null){
                        classroomCourseDetails.setNumber(i);
                    }
                    classroomCourseDetailsService.insert(classroomCourseDetails);
                    i++;
                }
            }
        }
        if (classroomCourse1==null){
            dto=DtoUtil.returnError(4002,"添加失败");
        }else {
            dto=DtoUtil.returnSuccessData(classroomCourse1);
        }
        return dto;
    }
    @GetMapping("lessonsByUid/{uid}")
    public Dto lessonsByUid(@PathVariable Integer uid){
        Dto dto=null;
        if (uid==null){
            dto=DtoUtil.returnError(4001,"参数错误");
            return dto;
        }
        List<ClassroomCourse> courses= classroomCourseService.selByLecture(uid);
        if (EmptyUtils.isNotEmpty(courses)){
            dto=DtoUtil.returnSuccessData(courses);
        }else {
            dto=DtoUtil.returnError(4002,"为空");
        }
        return dto;
    }

    @GetMapping("deBycourse/{id}")
    public Dto deBycourse(@PathVariable Integer id){
        Dto dto=null;
        if(id==null){
            dto=DtoUtil.returnError(4001,"参数错误");
            return dto;
        }
        boolean bool= classroomCourseService.deleteById(id);

        if (bool){
            dto=DtoUtil.returnSuccessData(bool);
        }else {
            dto=DtoUtil.returnError(4002,"删除失败");
        }
        return dto;
    }

    @GetMapping("upScore/{score}/{id}")
    public Dto upScore(@PathVariable Integer score,@PathVariable Integer id){
        Dto dto=null;
        if (score==null){
            dto=DtoUtil.returnError(4001,"参数错误");
            return dto;
        }
        ClassroomCourseVo classroomCourseVo=new ClassroomCourseVo();
//        classroomCourseVo.setScore(score);
        classroomCourseVo.setId(id);
        Boolean bool= classroomCourseService.updateScore(classroomCourseVo);
        if (bool)
            dto=DtoUtil.returnSuccessData(bool);
        else
            dto=DtoUtil.returnError(4002,"修改错误");
        return dto;
    }

    @GetMapping("addNum/{sid}/{id}/{uid}")
    public Dto addNum(@PathVariable Integer sid,@PathVariable Integer id,@PathVariable Integer uid){
        Dto dto=null;
        if (sid==null||id==null||uid==null){
            dto=DtoUtil.returnError(4001,"参数列表错误");
            return dto;
        }
        Boolean bool= classroomCourseDetailsService.countTheNumberOfLearners(sid,id,uid);
        dto=DtoUtil.returnSuccessData(bool);
        return dto;
    }
}